package com.example.gokchinesefoodmappcdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gokchinesefoodmappcdev.dao.CustomerInfoDao;
import com.example.gokchinesefoodmappcdev.dao.LocationAddressDao;
import com.example.gokchinesefoodmappcdev.dao.OrderInfoDao;
import com.example.gokchinesefoodmappcdev.entity.CustomerInfo;
import com.example.gokchinesefoodmappcdev.entity.LocationAddress;
import com.example.gokchinesefoodmappcdev.entity.OrderInfo;
import com.example.gokchinesefoodmappcdev.entity.PageBean;

import com.example.gokchinesefoodmappcdev.service.CustomerInfoService;
import com.example.gokchinesefoodmappcdev.util.ResultData;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {
    @Autowired
    private CustomerInfoDao customerInfoDao;
    @Autowired
    private LocationAddressDao locationAddressDao;
    @Autowired
    private OrderInfoDao orderInfoDao;

    /**
     * 根据条件查询所有用户
     * @param customerInfo 查询条件
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<CustomerInfo> findCustomerInfoAll(CustomerInfo customerInfo, int pageNum, int pageSize) {
        //使用PageHelper开启分页，该该方法调用需要在调用数据库的操作之前执行
        PageHelper.startPage(pageNum,pageSize);
        //查出数据结果集
        List<CustomerInfo>  customerInfos = customerInfoDao.findAllCustomerInfo(customerInfo);
        //获取总数据数
        int countCustomerInfoNum = customerInfoDao.countCustomerInfoNum(customerInfo);
        //将信息封装到PageBean中，返回出去
        PageBean<CustomerInfo> pageBean = new PageBean<>(pageNum,pageSize,countCustomerInfoNum);
        pageBean.setRows(customerInfos);

        return pageBean;
    }

    /**
     * 查询用户信息
     * @param customerId 用户id
     * @return
     */
    @Override
    public ResultData findCustomerInfoAndLocationAddressByCustomerId(Integer customerId) {
        // 查询用户个人信息
        QueryWrapper<CustomerInfo> customerInfowrapper = new QueryWrapper();
        customerInfowrapper.eq("customer_id",customerId);
        CustomerInfo customerInfo = customerInfoDao.selectOne(customerInfowrapper);
        // 查询用户的所有收货地址
        QueryWrapper<LocationAddress> locationAddressWrapper = new QueryWrapper();
        locationAddressWrapper.eq("customer_id",customerId);
        List<LocationAddress> locationAddressList = locationAddressDao.selectList(locationAddressWrapper);
        HashMap<String, Object> data = new HashMap<>();
        data.put("aderss",locationAddressList);
        data.put("user",customerInfo);

        return ResultData.createData(ResultData.OPERATION_SUCCESS,data);
    }

    /**
     * 修改用户状态
     * @param customerInfo
     * @return
     */
    @Override
    public ResultData updateCustomerInfoStatusByCustomerId(CustomerInfo customerInfo) {
        // 查询用户是否有未完成的订单
        QueryWrapper<OrderInfo> orderInfoWrapper = new QueryWrapper();
        List<Integer> statusList = new ArrayList<>();
        statusList.add(0);
        statusList.add(3);
        statusList.add(-3);
        orderInfoWrapper.notIn("order_status",statusList);
        orderInfoWrapper.eq("customer_id",customerInfo.getCustomerId());
        List<OrderInfo> orderInfoList = orderInfoDao.selectList(orderInfoWrapper);
        if (!orderInfoList.isEmpty()) {
            return ResultData.fail(ResultData.OPERATION_FAIL,"该用户有未完成订单，暂时无法禁用！");
        }
        // 修改用户的状态
        QueryWrapper<CustomerInfo> customerInfoWrapper = new QueryWrapper();
        customerInfoWrapper.eq("customer_id",customerInfo.getCustomerId());
        int i = customerInfoDao.update(customerInfo, customerInfoWrapper);
        if (i > 0) {
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"修改成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL,"修改失败");

    }

    /**
     * 导出用户列表
     * @param customerInfo
     * @return
     */
    @Override
    public List<CustomerInfo> outAllCustomerInfoExecl(CustomerInfo customerInfo) {
        List<CustomerInfo> customerInfos = customerInfoDao.outAllCustomerInfoNum(customerInfo);

        return customerInfos;
    }
}
